package com.example.myspringai.util;

import com.example.myspringai.model.LlmChatParam;
import com.example.myspringai.tangguomeng.OpenAIByTang;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExcelUtil {

    public static void writeExcel(List<List<String>> data, String sheetName) {
        System.out.println(data);
        // 创建新的工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建新的工作表
        XSSFSheet sheet = workbook.createSheet(sheetName);
        // 写入数据到工作表
        int rowNum = 0;
        for (List<String> rowData : data) {
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (String field : rowData) {
                Cell cell = row.createCell(colNum++);
                if (field == null) {
                    continue;
                }
                if (field.length() >= 32767) {
                    field = field.substring(0, 30000);
                }
                cell.setCellValue(field);
            }
        }
        // 将工作簿写入文件
        try (FileOutputStream outputStream = new FileOutputStream(sheetName + ".xlsx")) {
            workbook.write(outputStream);
            System.out.println("Excel 文件写入成功！");
        } catch (IOException e) {
            System.err.println("写入失败: " + e.getMessage());
        } finally {
            // 关闭工作簿
            try {
                workbook.close();
            } catch (IOException e) {
                System.err.println("关闭工作簿失败: " + e.getMessage());
            }
        }
    }

    public static class Risk {
        private String id;
        private String riskType;
        private String content;

        public Risk(String id, String riskType, String content) {
            this.id = id;
            this.riskType = riskType;
            this.content = content;
        }

        public String getId() {
            return id;
        }

        public String getRiskType() {
            return riskType;
        }

        public String getContent() {
            return content;
        }

        @Override
        public String toString() {
            return "Risk{" + "id=" + id + ", riskType='" + riskType + '\'' + ", content='" + content + '\'' + '}';
        }
    }

    public List<Risk> readExcelFile(String filePath) {
        List<Risk> risks = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue; // Skip the header row
                }
                String id = row.getCell(0) == null ? "" : String.valueOf(row.getCell(0).getNumericCellValue());
                String riskType = row.getCell(1) == null ? "" : String.valueOf(row.getCell(1).getStringCellValue());
                String content = row.getCell(2) == null ? "" : String.valueOf(row.getCell(2).getStringCellValue());
                risks.add(new Risk(id, riskType, content));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return risks;
    }

    public static void main(String[] args) {
        String filePath = "D:\\现场短信分类_new.xlsx";
        // 创建一个List数据
        List<List<String>> data =new ArrayList<>( Arrays.asList(
                Arrays.asList("语种","原生文本","中文翻译结果"))
        );
        try (FileInputStream fis = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue; // Skip the header row
                }
                if (row.getRowNum() > 10) {
                    continue; // Skip the header row
                }

                String content = row.getCell(0) == null ? "" : String.valueOf(row.getCell(0).getStringCellValue());
                if(StringUtils.isEmpty(content)){
                    continue;
                }
                List<String> rowData = new ArrayList<>();
                rowData.add(content);
                rowData.add(OpenAIByTang.message("请将以下翻译成中文： " +content));
                System.out.println(rowData);
                data.add(rowData);
            }
            ExcelUtil.writeExcel(data, "现场短信分类0-1000");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

